﻿/* 
* Copyright (c) 2010, The University of Texas at Austin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, 
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation and/or other materials 
* provided with the distribution.
*
* Neither the name of The University of Texas at Austin nor the names of its contributors may be 
* used to endorse or promote products derived from this software without specific prior written 
* permission.
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System.Diagnostics;
using System.Windows;
using System.Windows.Media;
using Bio.Data;
using Bio.Data.Providers.Structure;
using Bio.Views.Structure.Editor.Internal.Rendering;
using Bio.Views.Structure.Editor.Internal.Rendering.Visuals;

namespace Bio.Views.Structure.Editor.Internal.Editing
{
    public class BasePairVisualComponentPropertyContext : StructureElementVisualComponentPropertyContext
    {
        //Directly apply the latest property context state to the visual (this is basically how we support undo, assuming the "latest" property context was rolled back to reflect the undo).
        public override void Apply()
        {
            BpVisual.NucleotideFill = NucleotideFill;
            BpVisual.NucleotideFontFamily = NucleotideFontFamily;
            BpVisual.NucleotideFontStyle = NucleotideFontStyle;
            BpVisual.NucleotideFontWeight = NucleotideFontWeight;
            BpVisual.NucleotideFontSize = NucleotideFontSize;
            BpVisual.NucleotideRenderingStyle = NucleotideRenderingStyle;
            BpVisual.NucleotideSubstitution = NucleotideSubstitution;
            BpVisual.IsNucleotideSubstituted = IsNucleotideSubstituted;
            BpVisual.NucleotideCircleDiameter = NucleotideCircleDiameter;
            BpVisual.NucleotideCircleEdgeThickness = NucleotideCircleEdgeThickness;
            BpVisual.NucleotideCircleFill = NucleotideCircleFill;
            BpVisual.NucleotideCircleIsFilled = NucleotideCircleIsFilled;
            BpVisual.NucleotideCircleStroke = NucleotideCircleStroke;
            BpVisual.BasePairConnectorFill = BasePairConnectorFill;
            BpVisual.BasePairConnectorIsFilled = BasePairConnectorIsFilled;
            BpVisual.BasePairConnectorRenderingStyle = BasePairConnectorRenderingStyle;
            BpVisual.BasePairConnectorSize = BasePairConnectorSize;
            BpVisual.BasePairConnectorStroke = BasePairConnectorStroke;
            BpVisual.BasePairConnectorStrokeThickness = BasePairConnectorStrokeThickness;
            BpVisual.BasePairConnectorIsVisible = BasePairConnectorIsVisible;
        }

        //Save the latest property context state to a new metadata object.
        public override object Save()
        {
            Debug.Assert(BpVisual != null);
            Debug.Assert(BpVisual.Data != null);
            if (BpVisual.IsDirty())
            {
                BasePairMetadata bpDataObjMetadata = new BasePairMetadata();
                SaveNucleotideProperies(bpDataObjMetadata);
                SaveConnectorRenderingStyle(bpDataObjMetadata);
                SaveConnectorProperties(bpDataObjMetadata);
                return bpDataObjMetadata;
            }
            return null;
        }

        //Save the latest property context state to the data object.
        /*public override void Save()
        {
            Debug.Assert(BpVisual != null);
            Debug.Assert(BpVisual.Data != null);
            if (BpVisual.IsDirty())
            {
                IExtensibleProperties bpDataObjExt = BpVisual.Data as IExtensibleProperties;
                if (bpDataObjExt != null)
                {
                    BasePairMetadata bpDataObjMetadata = (bpDataObjExt.DoesExtendedPropertyExist(BasePairMetadata.Key)) ? bpDataObjExt.GetExtendedProperty<BasePairMetadata>(BasePairMetadata.Key) :
                        new BasePairMetadata();

                    SaveNucleotideProperies(bpDataObjMetadata);
                    SaveConnectorRenderingStyle(bpDataObjMetadata);
                    SaveConnectorProperties(bpDataObjMetadata);

                    if (!bpDataObjExt.DoesExtendedPropertyExist(BasePairMetadata.Key))
                        bpDataObjExt.SetExtendedProperty(BasePairMetadata.Key, bpDataObjMetadata);
                }
                else
                {
                    throw new NotSupportedException("Error: Saving Base Pair Visual Context is not possible as the Data Object does not support Metadata");
                }
            }
        }*/

        public BasePairVisualComponentPropertyContext(BasePairStructureElementVisualComponent bpVisual) 
            : base(bpVisual.IsComponentVisible)
        {
            Debug.Assert(bpVisual != null);
            Debug.Assert(bpVisual.Data != null);
            BpVisual = bpVisual;
            //We set the initial context directly to avoid tracking these as edits.
            CurrentContext.NucleotideFontFamily = BpVisual.GetValue(BasePairStructureElementVisualComponent.NucleotideFontFamilyProperty) as FontFamily;
            CurrentContext.NucleotideFontStyle = BpVisual.GetValue(BasePairStructureElementVisualComponent.NucleotideFontStyleProperty) as FontStyle?;
            CurrentContext.NucleotideFontWeight = BpVisual.GetValue(BasePairStructureElementVisualComponent.NucleotideFontWeightProperty) as FontWeight?;
            CurrentContext.NucleotideFontSize = BpVisual.GetValue(BasePairStructureElementVisualComponent.NucleotideFontSizeProperty) as double?;
            CurrentContext.NucleotideFill = BpVisual.GetValue(BasePairStructureElementVisualComponent.NucleotideFillProperty) as Brush;
            CurrentContext.NucleotideRenderingStyle = BpVisual.GetValue(BasePairStructureElementVisualComponent.NucleotideRenderingStyleProperty) as NucleotideRenderingMode?;
            CurrentContext.NucleotideSubstitution = BpVisual.GetValue(BasePairStructureElementVisualComponent.NucleotideSubstitutionProperty) as char?;
            CurrentContext.IsNucleotideSubstituted = BpVisual.GetValue(BasePairStructureElementVisualComponent.IsNucleotideSubstitutedProperty) as bool?;
            CurrentContext.NucleotideCircleDiameter = BpVisual.GetValue(BasePairStructureElementVisualComponent.NucleotideCircleDiameterProperty) as double?;
            CurrentContext.NucleotideCircleEdgeThickness = BpVisual.GetValue(BasePairStructureElementVisualComponent.NucleotideCircleEdgeThicknessProperty) as double?;
            CurrentContext.NucleotideCircleFill = BpVisual.GetValue(BasePairStructureElementVisualComponent.NucleotideCircleFillProperty) as Brush;
            CurrentContext.NucleotideCircleIsFilled = BpVisual.GetValue(BasePairStructureElementVisualComponent.NucleotideCircleIsFilledProperty) as bool?;
            CurrentContext.NucleotideCircleStroke = BpVisual.GetValue(BasePairStructureElementVisualComponent.NucleotideCircleStrokeProperty) as Brush;
            CurrentContext.BasePairConnectorFill = BpVisual.GetValue(BasePairStructureElementVisualComponent.BasePairConnectorFillProperty) as Brush;
            CurrentContext.BasePairConnectorIsFilled = BpVisual.GetValue(BasePairStructureElementVisualComponent.BasePairConnectorIsFilledProperty) as bool?;
            CurrentContext.BasePairConnectorIsVisible = BpVisual.GetValue(BasePairStructureElementVisualComponent.BasePairConnectorIsVisibleProperty) as bool?;
            CurrentContext.BasePairConnectorRenderingStyle = BpVisual.GetValue(BasePairStructureElementVisualComponent.BasePairConnectorRenderingStyleProperty) as BasePairConnectorRenderingMode?;
            CurrentContext.BasePairConnectorSize = BpVisual.GetValue(BasePairStructureElementVisualComponent.BasePairConnectorSizeProperty) as double?;
            CurrentContext.BasePairConnectorStroke = BpVisual.GetValue(BasePairStructureElementVisualComponent.BasePairConnectorStrokeProperty) as Brush;
            CurrentContext.BasePairConnectorStrokeThickness = BpVisual.GetValue(BasePairStructureElementVisualComponent.BasePairConnectorStrokeThicknessProperty) as double?;
        }

        private BasePairStructureElementVisualComponent BpVisual { get; set; }

        private void SaveConnectorRenderingStyle(BasePairMetadata bpDataObjMetadata)
        {
            if (BasePairConnectorRenderingStyle.HasValue)
            {
                switch (BasePairConnectorRenderingStyle.Value)
                {
                    case BasePairConnectorRenderingMode.Circle:
                        bpDataObjMetadata.ConnectorStyle = BasePairMetadata.ConnectorRenderingMode.Circle;
                        break;
                    case BasePairConnectorRenderingMode.Line:
                        bpDataObjMetadata.ConnectorStyle = BasePairMetadata.ConnectorRenderingMode.Line;
                        break;
                    case BasePairConnectorRenderingMode.Rectangle:
                        bpDataObjMetadata.ConnectorStyle = BasePairMetadata.ConnectorRenderingMode.Rectangle;
                        break;
                    default:
                        bpDataObjMetadata.ConnectorStyle = null;
                        break;
                };
            }
            else { bpDataObjMetadata.ConnectorStyle = null; }
        }

        private void SaveConnectorProperties(BasePairMetadata bpDataObjMetadata)
        {
            bpDataObjMetadata.ConnectorVisible = BasePairConnectorIsVisible;
            if (ContextHasConnectorRenderingPropertiesSet())
            {
                if (bpDataObjMetadata.ConnectorRendering == null)
                    bpDataObjMetadata.ConnectorRendering = new RNASecondaryStructureBasePairConnector();

                bpDataObjMetadata.ConnectorRendering.FillColor = (BasePairConnectorFill != null) ? BasePairConnectorFill.ToString() : null;
                bpDataObjMetadata.ConnectorRendering.Filled = BasePairConnectorIsFilled;
                bpDataObjMetadata.ConnectorRendering.Size = (BasePairConnectorSize.HasValue) ? BasePairConnectorSize.Value : 0.0;
                bpDataObjMetadata.ConnectorRendering.StrokeColor = (BasePairConnectorStroke != null) ? BasePairConnectorStroke.ToString() : null;
                bpDataObjMetadata.ConnectorRendering.EdgeThickness = (BasePairConnectorStrokeThickness.HasValue) ? BasePairConnectorStrokeThickness.Value : 0.0;
            }
            else { bpDataObjMetadata.ConnectorRendering = null; }
        }

        private void SaveNucleotideProperies(BasePairMetadata bpDataObjMetadata)
        {
            //Check if we have nucleotide rendering properties set.
            if (NucleotideRenderingStyle != null ||
                ContextHasCircleRenderingPropertiesSet() ||
                ContextHasTextRenderingPropertiesSet())
            {
                if (bpDataObjMetadata.NucleotideRenderingProperties == null)
                    bpDataObjMetadata.NucleotideRenderingProperties = new NucleotideMetadata();

                if (NucleotideRenderingStyle.HasValue)
                {
                    switch (NucleotideRenderingStyle.Value)
                    {
                        case NucleotideRenderingMode.Text:
                            bpDataObjMetadata.NucleotideRenderingProperties.Style = NucleotideMetadata.RenderingMode.Text;
                            break;
                        case NucleotideRenderingMode.Circle:
                            bpDataObjMetadata.NucleotideRenderingProperties.Style = NucleotideMetadata.RenderingMode.Circle;
                            break;
                        default:
                            bpDataObjMetadata.NucleotideRenderingProperties.Style = null;
                            break;
                    };
                }
                else
                {
                    bpDataObjMetadata.NucleotideRenderingProperties.Style = null;
                }

                if (ContextHasTextRenderingPropertiesSet())
                {
                    if (bpDataObjMetadata.NucleotideRenderingProperties.TextRendering == null)
                        bpDataObjMetadata.NucleotideRenderingProperties.TextRendering = new RNASecondaryStructureNucleotideTextShape();

                    bpDataObjMetadata.NucleotideRenderingProperties.TextRendering.Font = (NucleotideFontFamily != null) ? NucleotideFontFamily.Source : null;
                    bpDataObjMetadata.NucleotideRenderingProperties.TextRendering.FontStyle = (NucleotideFontStyle.HasValue) ? NucleotideFontStyle.Value.ToString() : null;
                    bpDataObjMetadata.NucleotideRenderingProperties.TextRendering.FontWeight = (NucleotideFontWeight.HasValue) ? NucleotideFontWeight.Value.ToString() : null;
                    bpDataObjMetadata.NucleotideRenderingProperties.TextRendering.FontSize = (NucleotideFontSize.HasValue) ? NucleotideFontSize.Value : 0.0;
                    bpDataObjMetadata.NucleotideRenderingProperties.TextRendering.Color = (NucleotideFill != null) ? NucleotideFill.ToString() : null;
                }
                else
                {
                    bpDataObjMetadata.NucleotideRenderingProperties.TextRendering = null;
                }

                if (ContextHasCircleRenderingPropertiesSet())
                {
                    if (bpDataObjMetadata.NucleotideRenderingProperties.CircleRendering == null)
                        bpDataObjMetadata.NucleotideRenderingProperties.CircleRendering = new RNASecondaryStructureNucleotideCircleShape();

                    bpDataObjMetadata.NucleotideRenderingProperties.CircleRendering.EdgeThickness = (NucleotideCircleEdgeThickness.HasValue && NucleotideCircleEdgeThickness.Value > 0.0) ? NucleotideCircleEdgeThickness.Value : 0.0;
                    bpDataObjMetadata.NucleotideRenderingProperties.CircleRendering.Diameter = (NucleotideCircleDiameter.HasValue && NucleotideCircleDiameter.Value > 0.0) ? NucleotideCircleDiameter.Value : 0.0;
                    bpDataObjMetadata.NucleotideRenderingProperties.CircleRendering.Filled = NucleotideCircleIsFilled;
                    bpDataObjMetadata.NucleotideRenderingProperties.CircleRendering.FillColor = (NucleotideCircleFill != null) ? NucleotideCircleFill.ToString() : null;
                    bpDataObjMetadata.NucleotideRenderingProperties.CircleRendering.EdgeColor = (NucleotideCircleStroke != null) ? NucleotideCircleStroke.ToString() : null;
                }
                else
                {
                    bpDataObjMetadata.NucleotideRenderingProperties.CircleRendering = null;
                }
            }
        }
    }
}
